from fastapi import APIRouter, Request
from fastapi import Depends
from requests import Session

from config.get_db import get_db
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsCarTrackVO
from core.service.cms_car_track_service import CmsCarTrackService
from utils.log_util import *
from utils.response_util import *

carTrackController = APIRouter()


@carTrackController.post("/car_track/query_by_id", response_model=CmsCarTrackVO)
async def get_car_track_by_id(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_car_track = CmsCarTrackService.get_cms_car_track_by_id(query_db, id)
        logger.info('获取成功')
        return response_200(data=cms_car_track, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carTrackController.post("/car_track/query_by_trace_id", response_model=CmsCarTrackVO)
async def get_car_track_by_trace_id(request: Request, trace_id: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_car_track = CmsCarTrackService.get_cms_car_track_by_track_id(query_db, trace_id)
        logger.info('获取成功')
        return response_200(data=cms_car_track, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carTrackController.post("/car_track/query_by_gaode_trace_id", response_model=CmsCarTrackVO)
async def get_car_track_by_gaode_trace_id(request: Request, gaode_trace_id: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_car_track_list = CmsCarTrackService.get_cms_car_track_by_gaode_track_id(query_db, gaode_trace_id)
        logger.info('获取成功')
        return response_200(data=cms_car_track_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carTrackController.post("/car_track/query_by_car_number", response_model=CmsCarTrackVO)
async def get_car_track_by_car_number(request: Request, gaode_trace_id: str, begin_time: datetime, end_time: datetime,
                                      query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_car_track_list = CmsCarTrackService.get_cms_car_track_by_car_number(query_db, gaode_trace_id, begin_time,
                                                                                end_time)
        logger.info('获取成功')
        return response_200(data=cms_car_track_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carTrackController.post("/car_track/query_like_trace_name", response_model=CmsCarTrackVO)
async def get_car_track_like_trace_name(request: Request, trace_name: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_car_track_list = CmsCarTrackService.get_cms_car_track_like_name(query_db, trace_name)
        logger.info('获取成功')
        return response_200(data=cms_car_track_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carTrackController.post("/car_track/add_track", response_model=CrudResponse)
async def add_car_track(request: Request, cms_car_track: CmsCarTrackVO, query_db: Session = Depends(get_db)):
    try:
        cms_car_track_result = await CmsCarTrackService.add_cms_car_track(query_db, cms_car_track)
        if cms_car_track_result.is_success:
            logger.info(cms_car_track_result.message)
            return response_200(data=cms_car_track_result, message=cms_car_track_result.message)
        else:
            logger.warning(cms_car_track_result.message)
            return response_400(data="", message=cms_car_track_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carTrackController.post("/car_track/delete", response_model=CrudResponse)
async def delete_car_track(request: Request, cms_car_track: CmsCarTrackVO, query_db: Session = Depends(get_db)):
    try:
        delete_car_track_result = CmsCarTrackService.delete_cms_car_track(query_db, cms_car_track.id,
                                                                          cms_car_track.service_id,
                                                                          cms_car_track.terminal_id,
                                                                          cms_car_track.gaode_track_id)
        if delete_car_track_result.is_success:
            logger.info(delete_car_track_result.message)
            return response_200(data=delete_car_track_result, message=delete_car_track_result.message)
        else:
            logger.warning(delete_car_track_result.message)
            return response_400(data="", message=delete_car_track_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
